# -*- coding: utf-8 -*-
"""
Created on Tue Nov 16 21:31:30 2021

@author: angelshare
"""

import pygame
import time
import random
import pickle

width = 512
height = 768
score = 0
enemy_list = []
bullet_list = []
supply_list = []
horizontal_speed = 3
vertical_speed = 3

# game status
start = 0
restart = -1

main_menu = 1
newgame_menu = 2
loadgame_menu = 3
settings_menu = 4
volume_menu = 5
gamelevel_menu = 6
end_menu = 7

game_status = main_menu
class Base(object):
    """所有类的基类"""

    def __init__(self, screen_temp, x, y, image_name):
        self.x = x
        self.y = y
        self.screen = screen_temp
        self.image = pygame.image.load(image_name).convert_alpha()
        self.w = self.image.get_rect().size[0]
        self.h = self.image.get_rect().size[1]


class BasePlane(Base):
    """飞机基类"""
    global bullet_list
    global enemy_list
    global width, height

    def __init__(self, plane_type, screen_temp, x, y, image, picture_num, HP_temp, authority):
        Base.__init__(self, screen_temp, x, y, image)  # plane_type飞机类型
        self.bullet_list = []  # 存储发射出去的子弹的引用
        self.plane_type = plane_type  # 飞机类型标示
        self.hp = HP_temp  # 飞机hp
        self.authority = authority
        self.time = 0
        # self.fire_bullet_count = 0#飞机已发射子弹计数
        self.explode_image = [pygame.transform.scale(pygame.image.load('image/explode/explode1.png').convert_alpha(), (self.w, self.h)),
                         pygame.transform.scale(pygame.image.load('image/explode/explode2.png').convert_alpha(), (self.w, self.h)),
                         pygame.transform.scale(pygame.image.load('image/explode/explode3.png').convert_alpha(), (self.w, self.h))]
        
    def display(self):
        global score
        if self.hp > 0:
            self.screen.blit(self.image, (self.x, self.y))
            self.hit()
        else:
            if self.time < 10:
                self.screen.blit(self.image, (self.x, self.y))
                self.screen.blit(self.explode_image[0], (self.x, self.y))
                self.time += 1
            elif self.time < 20:
                self.screen.blit(self.image, (self.x, self.y))
                self.screen.blit(self.explode_image[1], (self.x, self.y))
                self.time += 1
            elif self.time < 30:
                self.screen.blit(self.image, (self.x, self.y))
                self.screen.blit(self.explode_image[2], (self.x, self.y))
                self.time += 1
            else:
                if self.authority == 'enemy' and self.hp <= 0:
                    enemy_list.remove(self)
                    score += 1
        for item in self.bullet_list:
            if not item.judge():
                self.bullet_list.remove(item)

    def hit(self):
        if bullet_list and self.hp:
            for bullet in bullet_list:
                if bullet.authority != self.authority:
                    if bullet.x < self.x + self.w and bullet.x > self.x and bullet.y < self.y + self.h and bullet.y > self.y:
                        self.hp -= bullet.damage
                        bullet.damage = 0
                        bullet.visible = False
        if supply_list and self.hp:
            for supply in supply_list:
                if self.authority == 'player':
                    if supply.x < self.x + self.w and supply.x  + supply.w > self.x and supply.y < self.y + self.h and supply.y + supply.h > self.y:
                        self.hp += supply.capacity
                        if self.hp > self.hp_amount:
                            self.hp = self.hp_amount
                        supply.capacity = 0
                        supply.visible = False

    def pos_check(self):
        if self.x < 0:
            self.x = self.w
        elif self.x > width:
            self.x = 0
        if self.authority == 'player':
            if self.y < 0:
                self.y = 0
            elif self.y > height - self.h:
                self.y = height - self.h
        if self.authority == 'enemy' and self.y > height:
            enemy_list.remove(self)


class EnemyPlane(BasePlane):
    def __init__(self, screen_temp):
        image_list = ["image/enemyplane/enemyplane1.png", "image/enemyplane/enemyplane2.png", "image/enemyplane/enemyplane3.png"]
        random_num_x = random.randint(12, 418)
        random_num_y = random.randint(-100, -10)
        self.direction = "left"
        BasePlane.__init__(self, 0, screen_temp, random_num_x, random_num_y, image_list[random.randint(0, 2)], 4, 1, "enemy")
        
    def move(self):
        self.pos_check()
        self.fire()
        if self.hp > 0:
            if self.direction == 'right':
                self.x += 1
            elif self.direction == 'left':
                self.x -= 1
            if self.x > 430:
                self.direction = 'left'
            elif self.x < 0:
                self.direction = 'right'
            self.y += 3

    def fire(self):
        if len(self.bullet_list) != 0:
            if self.y - 14 - 80 > self.bullet_list[-1].y:
                self.bullet_list.append(Bullet(self.screen, self.x, self.y + 60, 'enemy'))
        else:
            self.bullet_list.append(Bullet(self.screen, self.x, self.y + 60, 'enemy'))

class BossPlane(BasePlane):
    def __init__(self, screen_temp):
        BasePlane.__init__(self, 0, screen_temp, 0, -100, "image/enemyplane/enemyplane4.png", 4, 20, "enemy")
        self.x = width / 2 - self.w / 2
        self.bar = Bar(screen_temp, int(width * 0.1), int(height * 0.1), int(width * 0.8), int(height * 0.02))

    def move(self):
        self.fire()
        if self.y < 100:
            self.y += 1

    def fire(self):
        if len(self.bullet_list) != 0:
            if self.y - 14 + 50 > self.bullet_list[-1].y:
                self.bullet_list.append(Bullet(self.screen, self.x, self.y + 60, 'enemy'))
                self.bullet_list.append(Bullet(self.screen, self.x + self.w / 2, self.y + 60, 'enemy'))
        else:
            self.bullet_list.append(Bullet(self.screen, self.x, self.y + 60, 'enemy'))
            self.bullet_list.append(Bullet(self.screen, self.x + self.w / 2, self.y + 60, 'enemy'))

class PlayerPlane(BasePlane):
    global supply_size
    def __init__(self, screen_temp, x, y, player_no):
        self.image_list = ["image/plane/plane1.png", "image/plane/plane2.png", "image/plane/plane3.png", "image/plane/plane4.png"]
        BasePlane.__init__(self, 3, screen_temp, x - 40, y - 100, self.image_list[0], 4, 100, "player") #super().__init__()
        self.player_no = player_no
        self.level = 0
        self.levelup_flag = False
        self.levelup_time = 0
        self.exp = 0
        self.hp_amount = 100
        self.move_dict = {'horizontal': 0, 'vertical': 0, 'space': 0}
        self.levelup_image = [pygame.transform.scale(pygame.image.load('image/levelup/1.png').convert_alpha(), (self.w, self.h)),
                         pygame.transform.scale(pygame.image.load('image/levelup/2.png').convert_alpha(), (self.w, self.h)),
                         pygame.transform.scale(pygame.image.load('image/levelup/3.png').convert_alpha(), (self.w, self.h)),
                         pygame.transform.scale(pygame.image.load('image/levelup/4.png').convert_alpha(), (self.w, self.h))]


    def levelup(self):
        if(score <= 150):
            if int(score / 50) > self.level :
                self.level = int(score / 50)
                self.levelup_flag = True
                self.image = pygame.image.load(self.image_list[self.level]).convert_alpha()
        if self.levelup_flag:
            if self.levelup_time < 20:
                self.screen.blit(self.image, (self.x, self.y))
                self.screen.blit(self.levelup_image[0], (self.x, self.y))
                self.levelup_time += 1
            elif self.levelup_time < 40:
                self.screen.blit(self.image, (self.x, self.y))
                self.screen.blit(self.levelup_image[1], (self.x, self.y))
                self.levelup_time += 1
            elif self.levelup_time < 60:
                self.screen.blit(self.image, (self.x, self.y))
                self.screen.blit(self.levelup_image[2], (self.x, self.y))
                self.levelup_time += 1
            elif self.levelup_time < 80:
                self.screen.blit(self.image, (self.x, self.y))
                self.screen.blit(self.levelup_image[3], (self.x, self.y))
                self.levelup_time += 1
            else:
                self.levelup_flag = False
                self.levelup_time = 0
                
    def move(self, horizontal_speed, vertical_speed):
        self.levelup()
        self.pos_check()
        if self.move_dict['horizontal'] != 0 and self.move_dict['vertical'] != 0:
            self.x += horizontal_speed * self.move_dict['horizontal']
            self.y += vertical_speed * self.move_dict['vertical']
        elif self.move_dict['horizontal'] != 0:
            self.x += horizontal_speed * self.move_dict['horizontal']
        elif self.move_dict['vertical'] != 0:
            self.y += vertical_speed * self.move_dict['vertical']
        if self.move_dict['space'] == 1:
            if len(self.bullet_list) != 0:
                if self.y - 14 - 80 > self.bullet_list[-1].y:
                    self.bullet_list.append(Bullet(self.screen, self.x, self.y, 'player'))
            else:
                self.bullet_list.append(Bullet(self.screen, self.x, self.y, 'player'))


class Bullet(Base):
    def __init__(self, screen_temp, x, y, authority):
        self.x = x + 40
        self.y = y - 20
        self.screen = screen_temp
        self.image = pygame.image.load("image/bullet/bullet2.png")
        self.authority = authority
        self.damage = 1
        self.visible = True
        if self.authority == 'enemy':
            self.image = pygame.transform.rotate(self.image, 180)

    def display(self):
        if self.visible:
            self.screen.blit(self.image, (self.x, self.y))

    def judge(self):
        if 0 < self.y < 797:
            return True
        else:
            return False

    def move(self):
        if self.authority == 'player':
            self.y -= 8
        elif self.authority == 'enemy':
            self.y += 8

class Supply(Base):
    def __init__(self, screen_temp):
        random_num_x = random.randint(12, 418)
        random_num_y = random.randint(-100, -10)
        super().__init__(screen_temp, random_num_x, random_num_y, "image/supply/hpsupply.png")
        self.capacity = random.randint(1, 20)
        self.visible = True

    def display(self):
        if self.visible:
            self.screen.blit(self.image, (self.x, self.y))
            
    def judge(self):
        if self.y < 797:
            return True
        else:
            return False
        
    def move(self):
        self.y += 1
        
class Game(object):
    def __init__(self, screen, player_list, enemy_list):
        self.player_list = player_list
        self.enemy_list = enemy_list

    # 保存游戏
    def save_game(self):
        with open('save.dat', 'wb') as f:
            pickle.dump(self, f)

    # 读取游戏
    def load_game(self):
        with open('save.dat', 'rb+') as f:
            model = pickle.load(f)
        return model

# 血条
class Bar(object):
    def __init__(self, screen, x, y, w, h):
        self.x = x
        self.y = y
        self.w = w
        self.h = h
        self.screen = screen

    def display(self, percent):
        pygame.draw.rect(self.screen, (178, 178, 178), (self.x, self.y, self.w, self.h))
        pygame.draw.rect(self.screen, (255, 0, 0), (self.x, self.y, self.w * percent, self.h))
        
# 标签
class Label(object):
    def __init__(self, screen, x, y, w, h):
        self.x = x
        self.y = y
        self.w = w
        self.h = h
        self.screen = screen

    def display(self, text, size=32):
        font = pygame.font.SysFont('Times New Romans', size)  # 设置字体及大小
        textSurf = font.render(text, True, (0, 0, 0))
        textRect = textSurf.get_rect()
        textRect.center = ((self.x + (self.w / 2)), (self.y + (self.h / 2)))
        self.screen.blit(textSurf, textRect)  # 绘制标签


# 按钮
class Button(object):
    def __init__(self, screen, x, y, w, h, text):
        self.x = x
        self.y = y
        self.w = w
        self.h = h
        self.text = text
        self.screen = screen

    def display(self):
        mouse_pos = pygame.mouse.get_pos()
        if mouse_pos[0] < self.x + self.w and mouse_pos[0] > self.x and mouse_pos[1] < self.y + self.h and mouse_pos[
            1] > self.y:
            pygame.draw.rect(self.screen, (178, 178, 178),
                             (self.x, self.y, self.w, self.h))  # 鼠标进入按钮，颜色设置为（178，178，178）
        else:
            pygame.draw.rect(self.screen, (128, 128, 128),
                             (self.x, self.y, self.w, self.h))  # 鼠标离开按钮，颜色设置为（128，128，128）
        font = pygame.font.SysFont('宋体', 32)  # 设置字体及大小
        textSurf = font.render(self.text, True, (0, 0, 0))
        textRect = textSurf.get_rect()
        textRect.center = ((self.x + (self.w / 2)), (self.y + (self.h / 2)))
        self.screen.blit(textSurf, textRect)  # 绘制按钮

    def click(self):
        mouse_pos = pygame.mouse.get_pos()
        if mouse_pos[0] < self.x + self.w and mouse_pos[0] > self.x and mouse_pos[1] < self.y + self.h and mouse_pos[
            1] > self.y:
            return True

def sum_bullet(player_list, enemy_list):
    global bullet_list
    for item in player_list:
        bullet_list.extend(item.bullet_list)
    for item in enemy_list:
        bullet_list.extend(item.bullet_list)
    return bullet_list


def create_enemy(screen):
    global enemy_list
    if len(enemy_list) < 5:
        enemy_list.append(EnemyPlane(screen))

def create_supply(screen):
    global supply_list
    if len(supply_list) < 1:
        supply_list.append(Supply(screen))
        
def game_fail():
    pass


def pause(player):
    button_list = [Button(player.screen, 160, 200, 160, 100, 'BackToGame'),
                   Button(player.screen, 160, 300, 160, 100, 'BackToMenu'),
                   Button(player.screen, 160, 400, 160, 100, 'Save'),
                   Button(player.screen, 160, 500, 160, 100, 'Exit')]
    isPause = True
    while isPause:
        for item in button_list:
            item.display()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
            elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                if button_list[0].click():
                    isPause = False
                    break
                elif button_list[1].click():
                    game_status = main_menu
                elif button_list[2].click():
                    pass
                elif button_list[3].click():
                    pygame.quit()
                    quit()

            elif event.type == event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                isPause = False
                break
        controller_num = pygame.joystick.get_count()
        if (controller_num != 0):
            controller = pygame.joystick.Joystick(0)
            controller.init()
            if controller.get_button(7):
                isPause = False
        pygame.display.update()
        time.sleep(0.01)


def ai_save_data():
    pass


# 自动游戏，ai控制移动和开火
def ai_control(player, event):
    '''model = load_model('lstm_300.h5', custom_objects={'r2': r2})
    player.move_dict['horizontal'] = model.predict(test_x)'''
    pass


# 键盘控制移动和开火
# 玩家一：WASD+空格
# 玩家二：方向键+小键盘回车键
def key_control(player, event):
    global isPause
    if event.type == pygame.KEYDOWN:
        if player:
            if player.player_no == 0:
                if event.key == pygame.K_a:
                    player.move_dict['horizontal'] = -1
                if event.key == pygame.K_d:
                    player.move_dict['horizontal'] = 1
                if event.key == pygame.K_w:
                    player.move_dict['vertical'] = -1
                if event.key == pygame.K_s:
                    player.move_dict['vertical'] = 1
                if event.key == pygame.K_SPACE:
                    player.move_dict['space'] = 1
            elif player.player_no == 1:
                if event.key == pygame.K_LEFT:
                    player.move_dict['horizontal'] = -1
                if event.key == pygame.K_RIGHT:
                    player.move_dict['horizontal'] = 1
                if event.key == pygame.K_UP:
                    player.move_dict['vertical'] = -1
                if event.key == pygame.K_DOWN:
                    player.move_dict['vertical'] = 1
                if event.key == pygame.K_KP_ENTER:
                    player.move_dict['space'] = 1
        if event.key == pygame.K_ESCAPE:  # 按下ESC键暂停游戏
            pause(player)

    elif event.type == pygame.KEYUP and player:
        if player.player_no == 0:
            if event.key == pygame.K_a:
                player.move_dict['horizontal'] = 0
            if event.key == pygame.K_d:
                player.move_dict['horizontal'] = 0
            if event.key == pygame.K_w:
                player.move_dict['vertical'] = 0
            if event.key == pygame.K_s:
                player.move_dict['vertical'] = 0
            if event.key == pygame.K_SPACE:
                player.move_dict['space'] = 0
        elif player.player_no == 1:
            if event.key == pygame.K_LEFT:
                player.move_dict['horizontal'] = 0
            if event.key == pygame.K_RIGHT:
                player.move_dict['horizontal'] = 0
            if event.key == pygame.K_UP:
                player.move_dict['vertical'] = 0
            if event.key == pygame.K_DOWN:
                player.move_dict['vertical'] = 0
            if event.key == pygame.K_KP_ENTER:
                player.move_dict['space'] = 0


# 手柄控制移动和开火
# 左摇杆+A（Xbox）/×（Playstation）/B（Nintendo）
def joystick_control(player, controller):
    if abs(controller.get_axis(0)) > 0.1:
        player.move_dict['horizontal'] = controller.get_axis(0)
    else:
        player.move_dict['horizontal'] = 0
    if abs(controller.get_axis(0)) > 0.1:
        player.move_dict['vertical'] = controller.get_axis(1)
    else:
        player.move_dict['vertical'] = 0
    if controller.get_button(0):
        player.move_dict['space'] = 1
    else:
        player.move_dict['space'] = 0
    if controller.get_button(7):
        pause(player)


def main():
    global width, height
    global score
    global enemy_list
    global bullet_list
    global supply_list

    pygame.init()  # 初始化pygame
    bg_size = width, height
    screen = pygame.display.set_mode(bg_size) # 显示窗口
    pygame.display.set_caption("飞机大战")

    
    #创建一个背景图片，路径需做出背景图片放入文件夹中填入路径
    background = pygame.image.load("image/background/background1.jpg").convert()

    score_label = Label(screen, width - 120, 0, 120, 30)
    pygame.joystick.init()
    controller_num = pygame.joystick.get_count()
    if controller_num != 0:
        controller = []
        for i in range(controller_num):
            controller.append(pygame.joystick.Joystick(i))
            controller[i].init()
    game_mode = 'single'
    game_volume = 2
    button_list = [Button(screen, width / 2 - 80, 350, 160, 100, 'New Game'),
                   Button(screen, width / 2 - 80, 450, 160, 100, 'Continue'),
                   Button(screen, width / 2 - 80, 550, 160, 100, 'Settings'),
                   Button(screen, width / 2 - 80, 650, 160, 100, 'Exit')]
    pygame.mixer.init()
    sound = pygame.mixer.Sound("music/bgm.wav")  # 播放
    sound.set_volume(game_volume / 10)  # 设置声音
    sound.play()  # 播放音乐

    game_status = main_menu
    game_level = 3
    pygame.display.update()
    while True:
        if game_status is end_menu:
            label1 = Label(screen, width / 2 - 120, height / 2 - 200, 240, 120)
            label2 = Label(screen, width / 2 - 60, height / 2 - 30, 120, 60)
            button1 = Button(screen, width / 2 - 80, height / 2 + 100, 160, 100, 'Restart')
            button2 = Button(screen, width / 2 - 80, height / 2 + 250, 160, 100, 'Back')
            while True:
                if game_status is not end_menu:
                    break
                label1.display('Game Over!', 72)
                label2.display('Your Score:' + str(score))
                button1.display()
                button2.display()
                pygame.display.update()
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        pygame.quit()
                    elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                        if button1.click():
                            game_status = start
                            score = 0
                            break
                        elif button2.click():
                            game_status = main_menu
                            button_list = [Button(screen, width / 2 - 80, 350, 160, 100, 'New Game'),
                                           Button(screen, width / 2 - 80, 450, 160, 100, 'Continue'),
                                           Button(screen, width / 2 - 80, 550, 160, 100, 'Settings'),
                                           Button(screen, width / 2 - 80, 650, 160, 100, 'Exit')]
                            break
        screen.blit(background, (0, 0))
        label = Label(screen, width / 2 - 120, height / 2 - 200, 240, 120)
        label.display('Plane War', 72)
        for button in button_list:
            button.display()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
            elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                if game_status is main_menu:
                    if button_list[0].click():  # newgame
                        button_list.clear()
                        button_list = [Button(screen, width / 2 - 80, 350, 160, 100, 'Single Player'),
                                       Button(screen, width / 2 - 80, 500, 160, 100, 'Double Player'),
                                       Button(screen, width / 2 - 80, 700, 160, 100, 'Back')]
                        game_status = newgame_menu
                        continue
                    elif button_list[1].click():  # load game 未实现
                        # game = Game.load_game()
                        pass
                    elif button_list[2].click():  # setting
                        button_list.clear()
                        button_list = [Button(screen, width / 2 - 80, 350, 160, 100, 'Volume'),
                                       Button(screen, width / 2 - 80, 500, 160, 100, 'Level'),
                                       Button(screen, width / 2 - 80, 700, 160, 100, 'Back')]
                        game_status = settings_menu
                        continue
                    elif button_list[3].click():  # exit
                        pygame.quit()
                elif game_status is newgame_menu:
                    if button_list[0].click():  # single game
                        game_status = start
                        game_mode = 'single'
                        break
                    elif button_list[1].click():  # double game
                        game_status = start
                        game_mode = 'double'
                        break
                    elif button_list[2].click(): # back to main menu
                        button_list.clear()
                        button_list = [Button(screen, width / 2 - 80, 350, 160, 100, 'New Game'),
                                       Button(screen, width / 2 - 80, 450, 160, 100, 'Continue'),
                                       Button(screen, width / 2 - 80, 550, 160, 100, 'Settings'),
                                       Button(screen, width / 2 - 80, 650, 160, 100, 'Exit')]
                        game_status = main_menu
                        continue
                elif game_status is loadgame_menu:
                    pass
                elif game_status is settings_menu:
                    if button_list[0].click():  # volume
                        button_list.clear()
                        button_list = [Button(screen, width / 2 - 80, 300, 160, 100, "nowVolume:" + str(game_volume)),
                                       Button(screen, width / 2 - 80, 450, 160, 100, 'increase'),
                                       Button(screen, width / 2 - 80, 550, 160, 100, 'decrease'),
                                       Button(screen, width / 2 - 80, 700, 160, 100, 'Back')]
                        game_status = volume_menu
                        continue
                    elif button_list[1].click():  # level
                        button_list.clear()
                        button_list = [Button(screen, width / 2 - 80, 300, 160, 100, "nowLevel:" + str(game_level)),
                                       Button(screen, width / 2 - 80, 450, 160, 100, 'increase'),
                                       Button(screen, width / 2 - 80, 550, 160, 100, 'decrease'),
                                       Button(screen, width / 2 - 80, 700, 160, 100, 'Back')]
                        game_status = gamelevel_menu
                        continue
                    elif button_list[2].click():  # back to main menu
                        button_list.clear()
                        button_list = [Button(screen, width / 2 - 80, 350, 160, 100, 'New Game'),
                                       Button(screen, width / 2 - 80, 450, 160, 100, 'Continue'),
                                       Button(screen, width / 2 - 80, 550, 160, 100, 'Settings'),
                                       Button(screen, width / 2 - 80, 650, 160, 100, 'Exit')]
                        game_status = main_menu
                        continue
                elif game_status is volume_menu:
                    if button_list[0].click():
                        if game_volume >= 4:
                            button_list[0].text = "('^`)"
                        else:
                            button_list[0].text = "O(^_^)O"
                    if button_list[1].click():  # increase
                        if game_volume + 1 <= 5:
                            game_volume = game_volume + 1
                            sound.set_volume(game_volume / 10)
                        button_list[0].text = "nowVolume:" + str(game_volume)
                    elif button_list[2].click():  # decrease
                        if game_volume - 1 >= 0:
                            game_volume = game_volume - 1
                            sound.set_volume(game_volume / 10)
                        button_list[0].text = "nowVolume:" + str(game_volume)
                    elif button_list[3].click():  # back
                        button_list.clear()
                        button_list = [Button(screen, width / 2 - 80, 350, 160, 100, 'Volume'),
                                       Button(screen, width / 2 - 80, 500, 160, 100, 'Level'),
                                       Button(screen, width / 2 - 80, 700, 160, 100, 'Back')]
                        game_status = settings_menu
                        continue
                elif game_status is gamelevel_menu:
                    if button_list[0].click():
                        if game_level >= 4:
                            button_list[0].text = "('^`)"
                        else:
                            button_list[0].text = "O(^_^)O"
                    if button_list[1].click():  # increase
                        if game_level + 1 <= 5:
                            game_level = game_level + 1
                        button_list[0].text = "nowLevel:" + str(game_level)
                    elif button_list[2].click():  # decrease
                        if game_level - 1 > 0:
                            game_level = game_level - 1
                        button_list[0].text = "nowLevel:" + str(game_level)
                    elif button_list[3].click():  # back
                        button_list.clear()
                        button_list = [Button(screen, width / 2 - 80, 350, 160, 100, 'Volume'),
                                       Button(screen, width / 2 - 80, 500, 160, 100, 'Level'),
                                       Button(screen, width / 2 - 80, 700, 160, 100, 'Back')]
                        game_status = settings_menu
                        continue

        if game_status is start:
            horizontal_speed = 10 - game_level
            vertical_speed = 10 - game_level
            #print(horizontal_speed)
            if game_mode == 'single':
                player_list = [PlayerPlane(screen, width / 2, height, player_no=0)]
                bar_list = [Bar(screen, int(width * 0.1), int(height * 0.9), int(width * 0.8), int(height * 0.02))]
            else:
                player_list = [PlayerPlane(screen, width / 3, height, player_no=0),
                               PlayerPlane(screen, width / 3 * 2, height, player_no=1)]
                bar_list = [Bar(screen, int(width * 0.1), int(height * 0.9), int(width * 0.35), int(height * 0.02)),
                            Bar(screen, int(width * 0.55), int(height * 0.9), int(width * 0.35), int(height * 0.02))]
            button_pause_text = "Pause"
            game_pause = False
            button_pause = Button(screen, width - 120, 30, 120, 30, button_pause_text)
            boss_num = 0
            
            while True:
                if game_status is main_menu:
                    print("main menu")
                    break
                button_pause = Button(screen, width - 120, 30, 120, 30, button_pause_text)
                pygame.display.update()
                screen.blit(background, (0, 0))
                button_pause.display()
                score_label.display('Score:' + str(score))

                # Game(screen, player_list, enemy_list)
                # Game.load_game()

                for i in range(len(player_list)):
                    player_list[i].display()
                    bar_list[i].display(player_list[i].hp / player_list[i].hp_amount)
                    
                create_enemy(screen)
                create_supply(screen)
                if int(score / 100) > boss_num:
                    enemy_list.append(BossPlane(screen))
                    boss_num += 1
                for enemy in enemy_list:
                    enemy.display()
                    enemy.move()
                    if isinstance(enemy, BossPlane):
                        enemy.bar.display(enemy.hp / 20)
                for supply in supply_list:
                    supply.display()
                    supply.move()
                bullet_list = []
                bullet_list = sum_bullet(player_list, enemy_list)
                # bullet_del_list = []
                for item in bullet_list:
                    if item.judge():
                        item.display()
                        item.move()
                for supply in supply_list:
                    if not supply.judge():
                        supply_list.remove(supply)
                '''    else:
                        bullet_del_list.append(item)
                for item in bullet_del_list:
                    bullet_list.remove(item)'''
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        pygame.quit()
                    if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                        if button_pause.click():
                            pause(player_list[0])
                            break

                    for player in player_list:
                        key_control(player, event)
                if player_list[0].hp <= 0:
                    player_list.clear()
                    enemy_list.clear()
                    supply_list.clear()
                    pygame.display.update()
                    game_status = end_menu
                    break

                if controller_num != 0:
                    joystick_control(player_list[0], controller[0])

                for player in player_list:
                    player.move(horizontal_speed, vertical_speed)
                time.sleep(0.01)
                pygame.display.update()
        pygame.display.update()
        time.sleep(0.02)

if __name__ == "__main__":
    main()
